{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "185d14b3",
   "metadata": {},
   "source": [
    "# 用bool返回一个值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "abf581c0",
   "metadata": {},
   "outputs": [],
   "source": [
    "def search4vowels(word):\n",
    "    \"\"\"display any vowels found in a supplied word.\"\"\"\n",
    "    vowels = set('aeiou')\n",
    "    found = vowels&set(word)\n",
    "    return bool(found)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "7451c503",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "search4vowels(\"hitch-hiker\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dd50493e",
   "metadata": {},
   "source": [
    "# 返回多个值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "bc062f86",
   "metadata": {},
   "outputs": [],
   "source": [
    "def search4vowels(word):\n",
    "    \"\"\"display any vowels found in a supplied word.\"\"\"\n",
    "    vowels = set('aeiou')\n",
    "    return vowels&set(word)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "deb6a2ba",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'e', 'i'}"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "search4vowels(\"hitch-hiker\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "80de7519",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "set()"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "search4vowels(\"sky\")    #  set()指的是返回一个空集合"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6c3b105c",
   "metadata": {},
   "source": [
    "# 回忆内置数据结构 "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "78b63ee2",
   "metadata": {},
   "source": [
    "## 列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "ae9b46d9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[]"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "one = list()\n",
    "one"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "089d46ea",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3]"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "one = [1, 2, 3]\n",
    "one"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a69a3137",
   "metadata": {},
   "source": [
    "## 元组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "65a534d0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "()"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "two = tuple()\n",
    "two"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "4e10f5bb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1, 2, 3)"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "two = (1, 2, 3)\n",
    "two"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "43b4efc7",
   "metadata": {},
   "source": [
    "## 字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "063a824a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{}"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "three = dict()\n",
    "three"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "1335074a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1: 'first', 2: 'second', 3: 'third'}"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "three = {1:\"first\",2:\"second\",3:\"third\"}\n",
    "three"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4a08a0f1",
   "metadata": {},
   "source": [
    "## 集合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "60b40450",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "set()"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "four = set()\n",
    "four"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "846c5a5d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1, 2, 3}"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "four = {1, 2, 3}\n",
    "four"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d7a0aa9d",
   "metadata": {},
   "source": [
    "# 使用注释改进文档"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "4697478a",
   "metadata": {},
   "outputs": [],
   "source": [
    "def search4vowels(word:str) -> set:\n",
    "    \"\"\"display any vowels found in a supplied word.\"\"\"\n",
    "    vowels = set('aeiou')\n",
    "    return vowels&set(word)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "8f558fc9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'e', 'i'}"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "search4vowels(\"hitch-hiker\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d549feca",
   "metadata": {},
   "source": [
    "# 用help BIF 查看注释"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "ef8491b7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on function search4vowels in module __main__:\n",
      "\n",
      "search4vowels(word: str) -> set\n",
      "    display any vowels found in a supplied word.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(search4vowels)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9e52c826",
   "metadata": {},
   "source": [
    "# 创建两个参数值的函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "01c67783",
   "metadata": {},
   "outputs": [],
   "source": [
    "def search4letters(phrase:str, letters:str) -> set: \n",
    "    \"\"\"return a set of the 'letters' found in 'phrase'.\"\"\"\n",
    "    return set(letters)&set(phrase)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "8c72c864",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'e', 'i'}"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "search4letters(\"hitch-hiker\",\"aeiou\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f2d7896c",
   "metadata": {},
   "source": [
    "# 为参数指定默认值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "acbc96f7",
   "metadata": {},
   "outputs": [],
   "source": [
    "def search4letters(phrase:str, letters:str = \"aeiou\") -> set: \n",
    "    \"\"\"return a set of the 'letters' found in 'phrase'.\"\"\"\n",
    "    return set(letters)&set(phrase)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "fa335bdc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'e', 'i'}"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "search4letters(\"hitch-hiker\")   #在调用函数的代码没有提供其他值的情况下，会自动使用该默认值  同时这也是一种位置赋值"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b2a75424",
   "metadata": {},
   "source": [
    "# 关键字赋值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "16abb17c",
   "metadata": {},
   "outputs": [],
   "source": [
    "def search4letters(phrase:str, letters:str = \"aeiou\") -> set: \n",
    "    \"\"\"return a set of the 'letters' found in 'phrase'.\"\"\"\n",
    "    return set(letters)&set(phrase)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "ce6e6c45",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'c'}"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "search4letters(letters = \"abc\",phrase = \"hitch-hiker\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b903ca68",
   "metadata": {},
   "source": [
    "# 创建并调用模块"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "e78602cd",
   "metadata": {},
   "outputs": [],
   "source": [
    "import 调用所创建函数的模块"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "df7bf69d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'4', '5', '6', '7'}"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "调用所创建函数的模块.search(\"456\",\"567\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0f63c4e6",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
